// pages/regist/regist.js

var tcity = require("../../utils/citys.js");
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    sexs: [
      '男',
      '女'
    ],
    sexNum: 0,

    likes: [
      {
        like: '钓鱼',
        checked: false
      },
      {
        like: '书画',
        checked: false
      },
      {
        like: '养花',
        checked: false
      },
      {
        like: '音乐',
        checked: false
      },
      {
        like: '下棋',
        checked: false
      },
      {
        like: '旅游',
        checked: false
      },
      {
        like: '摄影',
        checked: false
      },
      {
        like: '其他',
        checked: false
      }
    ],
    likeNum: 0,

    name: '',
    sex: '男',
    age: '',
    place: '',
    like: '其他',
    phone: '',
    code: '',

    isClickMark: true,
    btnGetMark: '获取验证码',

    // 省市区三级联动
    provinces: [],
    province: "",
    citys: [],
    city: "",
    countys: [],
    county: '',
    value: [0, 0, 0],
    values: [0, 0, 0],
    condition: false
  },
  // 获取验证码
  getMark: function () {
    var self = this;
    var regular = /^((13|15|18|16|17|19)+\d{9})$/;
    if (self.data.phone == '') {
      wx.showToast({
        icon: 'none',
        title: '请输入手机号',
      })
      return;
    } else if (self.data.phone.length < 11) {
      wx.showToast({
        icon: 'none',
        title: '必须是11位的手机号',
      })
      return;
    } else if (!regular.test(self.data.phone)) {
      wx.showToast({
        icon: 'none',
        title: '请输入正确的手机号',
      })
      return;
    }
    //是否可以点击
    if (self.data.isClickMark) {
      var url = app.serverUrl + 'getCode'
      wx.request({
        url: url,
        method: 'post',
        data: {
          tel: self.data.phone
        },
        success: function (e) {
          var timeNum = 61;
          var myTime = setInterval(function () {
            if (timeNum < 1) {
              clearInterval(myTime);
              self.setData({
                btnGetMark: '获取验证码',
                isClickMark: true
              })
            } else {
              timeNum--;
              self.setData({
                btnGetMark: '已发送(' + timeNum + ')',
                isClickMark: false
              })
            }
          }, 1000)
        },
        fail: function () {
          wx.showToast({
            title: '获取验证码失败！',
          })
        }
      })

    }
  },
  checkboxChange: function (e) {
    var self = this;
    self.data.hobby = e.detail.value
    console.log('checkbox发生change事件，携带value值为：', self.data.hobby)
  },

  // 初始化数据
  initData: function () {
    var self = this;
    wx.request({
      url: app.serverUrl + 'personalData',
      method: 'post',
      data: {
        openid: app.openid
      },
      success: function (res) {
        console.log('会员信息初始化', res)

        self.setData({
          name: res.data.data.realname,
          sex: res.data.data.sex,
          age: res.data.data.age,
          place: res.data.data.province + res.data.data.city + res.data.data.country,
          like: res.data.data.hobby.toString(),
          hobby:res.data.data.hobby,
          phone: res.data.data.tel,
          // 初始化城市ui
          province: res.data.data.province,
          city: res.data.data.city,
          county: res.data.data.country,

        })
        if (res.data.data.province == '' || res.data.data.province==null){
          self.setData({
            province: '北京'
          })
        } 
        if (res.data.data.city == '' || res.data.data.city == null){
          self.setData({
            city: '北京市'
          })
        }
        if (res.data.data.county == '' || res.data.data.county == null){
          self.setData({
            county: '东城区'
          })
        }
        // 初始化性别ui
        if (res.data.data.sex == '女') {
          self.setData({
            sexNum:1
          })
        } else {
          self.setData({
            sexNum: 0
          })
        }
        // 初始化爱好ui
        var tLikes = self.data.likes;
        for (var i = 0; i < tLikes.length;i++){
          for (var j = 0; j < res.data.data.hobby.length;j++){
            if (res.data.data.hobby[j] == tLikes[i].like){
              tLikes[i].checked=true
            }
          }
        }
        self.setData({
          likes: tLikes
        })
      }
    })
  },
  // 修改
  updata:function(){
    var self = this;
    this.setData({
      place: this.data.province + this.data.city + this.data.county,
    })
    console.log("openid", app.openid)
    if (this.data.name == '' || this.data.name == null) {
      wx.showToast({
        title: '请输入姓名',
      })
    } else if (this.data.age == '' || this.data.age == null) {
      wx.showToast({
        title: '请输入年龄',
      })
    } else if (this.data.phone == '' || this.data.phone == null) {
      wx.showToast({
        title: '请输入手机号',
      })
    } else if (this.data.code == '' || this.data.code == null) {
      wx.showToast({
        title: '请输入验证码',
      })
    } else {
      // 提交
      wx.request({
        url: app.serverUrl + 'personalUpdate',
        method: 'post',
        data: {
          openid: app.openid,
          realname: self.data.name,
          tel: this.data.phone,
          province: this.data.province,
          city: this.data.city,
          country: this.data.county,
          sex: this.data.sex,
          age: this.data.age,
          hobby: self.data.hobby.toString(),
          code: this.data.code
        },
        success: function (res) {
          console.log(res);
          if (res.data.flag == 'lackof') {
            wx.showToast({
              title: '验证码有误',
              icon: 'none',
            })
          } else if (res.data.flag == 'lackoftime') {
            wx.showToast({
              title: '验证码过期',
              icon: 'none',
            })

          } else if (res.data.flag == app.suc) {
            wx.showToast({
              title: '注册成功！',
              success: function () {
                wx.switchTab({
                  url: '/pages/main/main',
                })
              }
            })

          } else {
            wx.showToast({
              icon: 'none',
              title: '注册失败',
            })
          }
        }
      })

    }
  },
  // 注册
  regist: function () {
    var self = this;
    this.setData({
      place: this.data.province + this.data.city + this.data.county,
    })
    console.log("openid", app.openid)
    if (this.data.name == '' || this.data.name == null) {
      wx.showToast({
        title: '请输入姓名',
      })
    } else if (this.data.age == '' || this.data.age == null) {
      wx.showToast({
        title: '请输入年龄',
      })
    } else if (this.data.phone == '' || this.data.phone == null) {
      wx.showToast({
        title: '请输入手机号',
      })
    } else if (this.data.code == '' || this.data.code == null) {
      wx.showToast({
        title: '请输入验证码',
      })
    } else {
      // 提交
      wx.request({
        url: app.serverUrl + 'personalDataAdd',
        method: 'post',
        data: {
          openid: app.openid,
          realname: self.data.name,
          tel: this.data.phone,
          province: this.data.province,
          city: this.data.city,
          country: this.data.county,
          sex: this.data.sex,
          age: this.data.age,
          hobby: self.data.hobby.toString(),
          code: this.data.code
        },
        success: function (res) {
          console.log(res);
          if (res.data.flag == 'lackof') {
            wx.showToast({
              title: '验证码有误',
              icon: 'none',
            })
          } else if (res.data.flag == 'lackoftime') {
            wx.showToast({
              title: '验证码过期',
              icon: 'none',
            })

          } else if (res.data.flag == app.suc) {
            wx.showToast({
              title: '注册成功！',
              success: function () {
                wx.switchTab({
                  url: '/pages/main/main',
                })
              }
            })

          } else {
            wx.showToast({
              icon: 'none',
              title: '注册失败',
            })
          }
        }
      })

    }

  },

  // 获取姓名
  getName: function (e) {
    console.log(e.detail.value)
    this.setData({
      name: e.detail.value
    })
  },
  // 获取年龄
  getAge: function (e) {
    console.log(e.detail.value)
    this.setData({
      age: e.detail.value
    })
  },
  // 获取手机号
  getPhone: function (e) {
    this.setData({
      phone: e.detail.value
    })
  },
  // 获取验证码
  getCone: function (e) {
    this.setData({
      code: e.detail.value
    })
  },
  // 性别
  bindSexChange: function (e) {
    this.setData({
      sexNum: e.detail.value,
      sex: this.data.sexs[e.detail.value]
    })
  },
  // 爱好
  bindLikeChange: function (e) {
    this.setData({
      likeNum: e.detail.value,
      like: this.data.likes[e.detail.value]
    })
  },

  //省市区三级联动
  bindcityChange: function (e) {
    var val = e.detail.value
    var t = this.data.values;
    var cityData = this.data.cityData;

    if (val[0] != t[0]) {
      console.log('province no ');
      const citys = [];
      const countys = [];

      for (let i = 0; i < cityData[val[0]].sub.length; i++) {
        citys.push(cityData[val[0]].sub[i].name)
      }
      for (let i = 0; i < cityData[val[0]].sub[0].sub.length; i++) {
        countys.push(cityData[val[0]].sub[0].sub[i].name)
      }

      this.setData({
        province: this.data.provinces[val[0]],
        city: cityData[val[0]].sub[0].name,
        citys: citys,
        county: cityData[val[0]].sub[0].sub[0].name,
        countys: countys,
        values: val,
        value: [val[0], 0, 0]
      })

      return;
    }
    if (val[1] != t[1]) {
      const countys = [];

      for (let i = 0; i < cityData[val[0]].sub[val[1]].sub.length; i++) {
        countys.push(cityData[val[0]].sub[val[1]].sub[i].name)
      }

      this.setData({
        city: this.data.citys[val[1]],
        county: cityData[val[0]].sub[val[1]].sub[0].name,
        countys: countys,
        values: val,
        value: [val[0], val[1], 0]
      })
      return;
    }
    if (val[2] != t[2]) {
      this.setData({
        county: this.data.countys[val[2]],
        values: val
      })
      return;
    }


  },
  open: function () {
    this.setData({
      condition: !this.data.condition
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // 省市区三级联动初始化
    var that = this;

    tcity.init(that);

    var cityData = that.data.cityData;


    const provinces = [];
    const citys = [];
    const countys = [];

    for (let i = 0; i < cityData.length; i++) {
      provinces.push(cityData[i].name);
    }

    for (let i = 0; i < cityData[0].sub.length; i++) {
      citys.push(cityData[0].sub[i].name)
    }
    for (let i = 0; i < cityData[0].sub[0].sub.length; i++) {
      countys.push(cityData[0].sub[0].sub[i].name)
    }

    that.setData({
      'provinces': provinces,
      'citys': citys,
      'countys': countys,
      'province': cityData[0].name,
      'city': cityData[0].sub[0].name,
      'county': cityData[0].sub[0].sub[0].name
    })

    // 初始化会员信息
    // this.initData();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return {
      title: '大易益·传统文化大舞台，爱好者的掌中宝！',
      path: '/pages/index/index'
    }
  }
})